*-------------------------------------------------------------------------------*
* DIRECTORIES
*-------------------------------------------------------------------------------*
local l_direc_data `1'
local l_direc_code `2' 
local l_direc_figtab `3' 

local direc_data_raw  `l_direc_data'/raw
local direc_data_clean `l_direc_data'/clean



*---------------------------------------------------------------------------------*
* DATA IN AND SAMPLE AND CLEAN
*---------------------------------------------------------------------------------*
use "`direc_data_clean'/ajt_regready.dta", clear

keep if year >= 1985

foreach l_var of varlist jshr_frcnt_all eshr2_frcnt_all {
	gen A_`l_var' = `l_var'>0
	gen lg_`l_var' = log(`l_var')
	ihstrans `l_var'
}

gen X = .
gen Y = .

*---------------------------------------------------------------------------------*
* MAIN TABLE: PANEL A -- linear
*---------------------------------------------------------------------------------*
preserve

	replace X = eshr2_frcnt_all
	replace Y = jshr_frcnt_all
	
	reghdfe Y X , a(fe_ja fe_at) 
	
	keep if e(sample)
	
	* NO FE
	eststo clear
	qui {
		reg Y X 
			local l_r2tot = e(r2)
		reg Y 
			local estflg_par2_X = (`l_r2tot'-e(r2))/(1-e(r2))
	}
	eststo: reg Y X , cluster(fe_a)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X {
			estadd scalar `l_flg' = ``l_flg''
		}
		tabstat jshr_frcnt_all eshr2_frcnt_all if e(sample), save
		estadd scalar estflg_elast = (_b[X]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])
		*tabstat jshr_frcnt_all eshr2_frcnt_all if e(sample), save
		*estadd scalar estflg_elast = (_b[X]*(sqrt((r(StatTotal)[1,1])^2+1)/r(StatTotal)[1,1])*(r(StatTotal)[1,2]/sqrt((r(StatTotal)[1,2])^2+1)))

		
	* JM	
	qui {
		reghdfe Y X , a(fe_ja) 
		local l_r2tot = e(r2)
		reghdfe Y X , noabsorb
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a(fe_ja)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_ja) cluster(fe_a)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
		tabstat jshr_frcnt_all eshr2_frcnt_all if e(sample), save
		estadd scalar estflg_elast = (_b[X]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])
		*tabstat jshr_frcnt_all eshr2_frcnt_all if e(sample), save
		*estadd scalar estflg_elast = (_b[X]*(sqrt((r(StatTotal)[1,1])^2+1)/r(StatTotal)[1,1])*(r(StatTotal)[1,2]/sqrt((r(StatTotal)[1,2])^2+1)))
		
	* MT	
	qui {
		reghdfe Y X , a(fe_at) 
		local l_r2tot = e(r2)
		reghdfe Y X , noabsorb
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a(fe_at)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_at) cluster(fe_a)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt {
			estadd scalar `l_flg' = ``l_flg''
		}
		tabstat jshr_frcnt_all eshr2_frcnt_all if e(sample), save
		estadd scalar estflg_elast = (_b[X]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])
		*tabstat jshr_frcnt_all eshr2_frcnt_all if e(sample), save
		*estadd scalar estflg_elast = (_b[X]*(sqrt((r(StatTotal)[1,1])^2+1)/r(StatTotal)[1,1])*(r(StatTotal)[1,2]/sqrt((r(StatTotal)[1,2])^2+1)))

	
	* JM MT
	qui {
		reghdfe Y X , a(fe_ja fe_at) 
		local l_r2tot = e(r2)
		reghdfe Y X , a(fe_at) 
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y X , a(fe_ja) 
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a( fe_ja fe_at)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_jt fe_ja fe_at) cluster(fe_a)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
		tabstat jshr_frcnt_all eshr2_frcnt_all if e(sample), save
		estadd scalar estflg_elast = (_b[X]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])
		*tabstat jshr_frcnt_all eshr2_frcnt_all if e(sample), save
		*estadd scalar estflg_elast = (_b[X]*(sqrt((r(StatTotal)[1,1])^2+1)/r(StatTotal)[1,1])*(r(StatTotal)[1,2]/sqrt((r(StatTotal)[1,2])^2+1)))
		
	esttab using"`l_direc_figtab'/tab_affilreg.tex", keep(X) label mlabels(none) ///
		b(%5.3fc) se(%5.3fc) star(* 0.1 ** 0.05 *** 0.01) ///
		scalars(estflg_elast estflg_r2tot estflg_par2_jm estflg_par2_mt estflg_par2_X ) ///
		sfmt(%5.3fc %5.3fc %5.3fc %5.3fc %5.3fc) nonotes ///
		style(tex) substitute(_ \_  $  \\$  %  \% ) ///
		replace
	eststo clear	
restore



*---------------------------------------------------------------------------------*
* MAIN TABLE: PANEL B -- 0/1
*---------------------------------------------------------------------------------*
preserve

	replace X = A_eshr2_frcnt_all
	replace Y = A_jshr_frcnt_all
	
	reghdfe Y X , a(fe_ja fe_at) 
	
	keep if e(sample)
	
	* NO FE
	eststo clear
	qui {
		reg Y X 
			local l_r2tot = e(r2)
		reg Y 
			local estflg_par2_X = (`l_r2tot'-e(r2))/(1-e(r2))
	}
	eststo: reg Y X , cluster(fe_a)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X {
			estadd scalar `l_flg' = ``l_flg''
		}
		tabstat A_jshr_frcnt_all A_eshr2_frcnt_all if e(sample), save
		estadd scalar estflg_elast = (_b[X]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])
		
	* JM	
	qui {
		reghdfe Y X , a(fe_ja) 
		local l_r2tot = e(r2)
		reghdfe Y X , noabsorb
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a(fe_ja)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_ja) cluster(fe_a)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
		tabstat A_jshr_frcnt_all A_eshr2_frcnt_all if e(sample), save
		estadd scalar estflg_elast = (_b[X]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])
		
	* MT	
	qui {
		reghdfe Y X , a(fe_at) 
		local l_r2tot = e(r2)
		reghdfe Y X , noabsorb
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a(fe_at)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_at) cluster(fe_a)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt {
			estadd scalar `l_flg' = ``l_flg''
		}
		tabstat A_jshr_frcnt_all A_eshr2_frcnt_all if e(sample), save
		estadd scalar estflg_elast = (_b[X]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])
	
	* JM MT
	qui {
		reghdfe Y X , a(fe_ja fe_at) 
		local l_r2tot = e(r2)
		reghdfe Y X , a(fe_at) 
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y X , a(fe_ja) 
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a( fe_ja fe_at)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_jt fe_ja fe_at) cluster(fe_a)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
		tabstat A_jshr_frcnt_all A_eshr2_frcnt_all if e(sample), save
		estadd scalar estflg_elast = (_b[X]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])
		
	esttab using"`l_direc_figtab'/tab_affilreg.tex", keep(X) label mlabels(none) ///
		b(%5.3fc) se(%5.3fc) star(* 0.1 ** 0.05 *** 0.01) ///
		scalars(estflg_elast estflg_r2tot estflg_par2_jm estflg_par2_mt estflg_par2_X ) ///
		sfmt(%5.3fc %5.3fc %5.3fc %5.3fc %5.3fc) nonotes ///
		style(tex) substitute(_ \_  $  \\$  %  \% ) ///
		append
	eststo clear	
restore



*---------------------------------------------------------------------------------*
* MAIN TABLE: PANEL C -- LOG
*---------------------------------------------------------------------------------*
preserve

	replace X = lg_eshr2_frcnt_all
	replace Y = lg_jshr_frcnt_all
	
	reghdfe Y X , a(fe_ja fe_at) 
	
	keep if e(sample)
	
	* NO FE
	eststo clear
	qui {
		reg Y X 
			local l_r2tot = e(r2)
		reg Y 
			local estflg_par2_X = (`l_r2tot'-e(r2))/(1-e(r2))
	}
	eststo: reg Y X , cluster(fe_a)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X {
			estadd scalar `l_flg' = ``l_flg''
		}
		estadd scalar estflg_elast = _b[X]
		
	* JM	
	qui {
		reghdfe Y X , a(fe_ja) 
		local l_r2tot = e(r2)
		reghdfe Y X , noabsorb
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a(fe_ja)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_ja) cluster(fe_a)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
		estadd scalar estflg_elast = _b[X]
		
	* MT	
	qui {
		reghdfe Y X , a(fe_at) 
		local l_r2tot = e(r2)
		reghdfe Y X , noabsorb
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a(fe_at)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_at) cluster(fe_a)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt {
			estadd scalar `l_flg' = ``l_flg''
		}
		estadd scalar estflg_elast = _b[X]
	
	* JM MT
	qui {
		reghdfe Y X , a(fe_ja fe_at) 
		local l_r2tot = e(r2)
		reghdfe Y X , a(fe_at) 
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y X , a(fe_ja) 
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a( fe_ja fe_at)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_jt fe_ja fe_at) cluster(fe_a)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
		estadd scalar estflg_elast = _b[X]
		
	esttab using"`l_direc_figtab'/tab_affilreg.tex", keep(X) label mlabels(none) ///
		b(%5.3fc) se(%5.3fc) star(* 0.1 ** 0.05 *** 0.01) ///
		scalars(estflg_elast estflg_r2tot estflg_par2_jm estflg_par2_mt estflg_par2_X ) ///
		sfmt(%5.3fc %5.3fc %5.3fc %5.3fc %5.3fc) nonotes ///
		style(tex) substitute(_ \_  $  \\$  %  \% ) ///
		append
	eststo clear	
restore	







	
	
	
